package com.ms.qw.jsf.converter;

import com.ms.support.mapper.DTOMapperService;
import com.ms.qw.application.mappers.CheckPointMapper;
import com.ms.qw.client.dto.CheckPointDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 * User: aglwkrs
 * Date: 30/11/11
 */
@Component
@FacesConverter("com.ms.qw.jsf.converter.CheckPointDTOConverter")
public class CheckPointDTOConverter implements Converter {

    @Autowired
    protected CheckPointMapper checkPointMapper;


    @Autowired
    private DTOMapperService mapperService;


    public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
        if (StringUtils.isBlank(value) || value.equals("Seleccione")|| value.equals("Todos")) {
            return null;
        }
        CheckPointDTO item= checkPointMapper.findById(new Long(value));
        return item;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
        if (value == null ) {
            return null;
        }

        if(value instanceof CheckPointDTO){
            return ((CheckPointDTO)value).getCheckPointId()+"";
        }
        return null;

    }
}
